
****INDIVIDUAL FORECASTS 
eststo clear

use "$jfqa_rep/21_DJIA_convexity_20.dta", clear

qui{
merge 1:m year month day using "$jfqa_rep/Shiller_Individual_Ests.dta"
keep if _merge==3
drop _merge

sort year month day

gen monthyear=ym(year, month)

drop if abs(dow_1m)>=.5

gen wret_1=(1+ret_5)*(1+ret_1)*(1+ret_2)*(1+ret_3)*(1+ret_4)-1
gen wret_2=(1+ret_10)*(1+ret_6)*(1+ret_7)*(1+ret_8)*(1+ret_9)-1
gen wret_3=(1+ret_15)*(1+ret_11)*(1+ret_12)*(1+ret_13)*(1+ret_14)-1
gen wret_4=(1+ret_20)*(1+ret_16)*(1+ret_17)*(1+ret_18)*(1+ret_19)-1

gen futwret_1=(1+futret_1)*(1+futret_2)*(1+futret_3)*(1+futret_4)*(1+futret_5)-1
gen futwret_2=(1+futret_6)*(1+futret_7)*(1+futret_8)*(1+futret_9)*(1+futret_10)-1
gen futwret_3=(1+futret_11)*(1+futret_12)*(1+futret_13)*(1+futret_14)*(1+futret_15)-1
gen futwret_4=(1+futret_16)*(1+futret_17)*(1+futret_18)*(1+futret_19)*(1+futret_20)-1
gen futmret_1=(1+futwret_1)*(1+futwret_2)*(1+futwret_3)*(1+futwret_4)*(1+futret_21)-1

collapse lag_convexity wret* dow_1m fut*, by(year month day monthyear)

gen obs=_n

gen fit_expect_4=.
tsset obs


*4 lags
forval i=500/2538 {
nl (dow_1m = {b0} + ///
	{b1}*(({b2}*wret_1)/({b2}+{b2}^2+{b2}^3+{b2}^4) + ///
		({b2}^2*wret_2)/({b2}+{b2}^2+{b2}^3+{b2}^4) + ///
		({b2}^3*wret_3)/({b2}+{b2}^2+{b2}^3+{b2}^4) + ///
		({b2}^4*wret_4)/({b2}+{b2}^2+{b2}^3+{b2}^4))) if obs<(`i'+1), ///
		initial(b0 0 b1 1 b2 0.5) nolog vce(cluster monthyear)

gen fit_expect=_b[/b0] + _b[/b1]*((_b[/b2]*wret_1)/(_b[/b2]+_b[/b2]^2+_b[/b2]^3+_b[/b2]^4) + ///
		(_b[/b2]^2*wret_2)/(_b[/b2]+_b[/b2]^2+_b[/b2]^3+_b[/b2]^4) + ///
		(_b[/b2]^3*wret_3)/(_b[/b2]+_b[/b2]^2+_b[/b2]^3+_b[/b2]^4) + ///
		(_b[/b2]^4*wret_4)/(_b[/b2]+_b[/b2]^2+_b[/b2]^3+_b[/b2]^4)) if obs<(`i'+1)
	
replace fit_expect_4=fit_expect if obs==`i'

drop fit_expect
}

drop if missing(fit_expect_4)

replace fit_expect_4=fit_expect_4*100
replace futmret_1=futmret_1*100
replace dow_1m=dow_1m*100

gen res_expect_4=dow_1m-fit_expect_4
egen res_expect_4_std=std(res_expect_4)
egen fit_expect_4_std=std(fit_expect_4)
egen dow_1m_std=std(dow_1m)
egen convex_std=std(lag_convexity)
}

****TABLE 10****
*coefficients and t-stats
newey futmret_1 dow_1m_std, lag(8)
newey futmret_1 fit_expect_4_std, lag(8)
newey futmret_1 res_expect_4_std, lag(8)

*R-squared
reg futmret_1 dow_1m_std
reg futmret_1 fit_expect_4_std
reg futmret_1 res_expect_4_std

****TABLE 11****
*coefficients and t-stats
newey dow_1m convex_std, lag(8)
newey fit_expect_4 convex_std, lag(8)
newey res_expect_4 convex_std, lag(8)

*R-squared
reg dow_1m convex_std
reg fit_expect_4 convex_std
reg res_expect_4 convex_std

****INSTITUTIONAL FORECASTS 
use "$jfqa_rep/21_DJIA_convexity_20.dta", clear

qui{
merge 1:m year month day using "$jfqa_rep/Shiller_Inst_Ests.dta"
keep if _merge==3
drop _merge

sort year month day

gen monthyear=ym(year, month)

drop if year<1993

drop if abs(dow_1m)>=.5

gen wret_1=(1+ret_5)*(1+ret_1)*(1+ret_2)*(1+ret_3)*(1+ret_4)-1
gen wret_2=(1+ret_10)*(1+ret_6)*(1+ret_7)*(1+ret_8)*(1+ret_9)-1
gen wret_3=(1+ret_15)*(1+ret_11)*(1+ret_12)*(1+ret_13)*(1+ret_14)-1
gen wret_4=(1+ret_20)*(1+ret_16)*(1+ret_17)*(1+ret_18)*(1+ret_19)-1

gen futwret_1=(1+futret_1)*(1+futret_2)*(1+futret_3)*(1+futret_4)*(1+futret_5)-1
gen futwret_2=(1+futret_6)*(1+futret_7)*(1+futret_8)*(1+futret_9)*(1+futret_10)-1
gen futwret_3=(1+futret_11)*(1+futret_12)*(1+futret_13)*(1+futret_14)*(1+futret_15)-1
gen futwret_4=(1+futret_16)*(1+futret_17)*(1+futret_18)*(1+futret_19)*(1+futret_20)-1
gen futmret_1=(1+futwret_1)*(1+futwret_2)*(1+futwret_3)*(1+futwret_4)*(1+futret_21)-1

collapse lag_convexity wret* dow_1m fut*, by(year month day monthyear)

gen obs=_n

gen fit_expect_4=.
tsset obs

*4 lags
forval i=500/2529 {
nl (dow_1m = {b0} + ///
	{b1}*(({b2}*wret_1)/({b2}+{b2}^2+{b2}^3+{b2}^4) + ///
		({b2}^2*wret_2)/({b2}+{b2}^2+{b2}^3+{b2}^4) + ///
		({b2}^3*wret_3)/({b2}+{b2}^2+{b2}^3+{b2}^4) + ///
		({b2}^4*wret_4)/({b2}+{b2}^2+{b2}^3+{b2}^4))) if obs<(`i'+1), ///
		initial(b0 0 b1 1 b2 0.5) nolog vce(cluster monthyear)

gen fit_expect=_b[/b0] + _b[/b1]*((_b[/b2]*wret_1)/(_b[/b2]+_b[/b2]^2+_b[/b2]^3+_b[/b2]^4) + ///
		(_b[/b2]^2*wret_2)/(_b[/b2]+_b[/b2]^2+_b[/b2]^3+_b[/b2]^4) + ///
		(_b[/b2]^3*wret_3)/(_b[/b2]+_b[/b2]^2+_b[/b2]^3+_b[/b2]^4) + ///
		(_b[/b2]^4*wret_4)/(_b[/b2]+_b[/b2]^2+_b[/b2]^3+_b[/b2]^4)) if obs<(`i'+1)
	
replace fit_expect_4=fit_expect if obs==`i'

drop fit_expect

}

drop if missing(fit_expect_4)

replace fit_expect_4=fit_expect_4*100
replace futmret_1=futmret_1*100
replace dow_1m=dow_1m*100

gen res_expect_4=dow_1m-fit_expect_4
egen res_expect_4_std=std(res_expect_4)
egen fit_expect_4_std=std(fit_expect_4)
egen dow_1m_std=std(dow_1m)
egen convex_std=std(lag_convexity)
}

****TABLE 10****
*coefficients and t-stats
newey futmret_1 dow_1m_std, lag(8)
newey futmret_1 fit_expect_4_std, lag(8)
newey futmret_1 res_expect_4_std, lag(8)

*R-squared
reg futmret_1 dow_1m_std
reg futmret_1 fit_expect_4_std
reg futmret_1 res_expect_4_std

****TABLE 11****
*coefficients and t-stats
newey dow_1m convex_std, lag(8)
newey fit_expect_4 convex_std, lag(8)
newey res_expect_4 convex_std, lag(8)

*R-squared
reg dow_1m convex_std
reg fit_expect_4 convex_std
reg res_expect_4 convex_std

****ALL FORECASTS 
eststo clear
use "$jfqa_rep/21_DJIA_convexity_20.dta", clear

qui{
merge 1:m year month day using "$jfqa_rep/Shiller_Inst_Ests.dta"
keep if _merge==3
drop _merge

preserve
tempfile copy

use "$jfqa_rep/21_DJIA_convexity_20.dta", clear

merge 1:m year month day using "$jfqa_rep/Shiller_Individual_Ests.dta"
keep if _merge==3
drop _merge

save `copy', replace

restore
append using `copy'

sort year month day

gen monthyear=ym(year, month)

drop if year<1993

drop if abs(dow_1m)>=.5

gen wret_1=(1+ret_5)*(1+ret_1)*(1+ret_2)*(1+ret_3)*(1+ret_4)-1
gen wret_2=(1+ret_10)*(1+ret_6)*(1+ret_7)*(1+ret_8)*(1+ret_9)-1
gen wret_3=(1+ret_15)*(1+ret_11)*(1+ret_12)*(1+ret_13)*(1+ret_14)-1
gen wret_4=(1+ret_20)*(1+ret_16)*(1+ret_17)*(1+ret_18)*(1+ret_19)-1

gen futwret_1=(1+futret_1)*(1+futret_2)*(1+futret_3)*(1+futret_4)*(1+futret_5)-1
gen futwret_2=(1+futret_6)*(1+futret_7)*(1+futret_8)*(1+futret_9)*(1+futret_10)-1
gen futwret_3=(1+futret_11)*(1+futret_12)*(1+futret_13)*(1+futret_14)*(1+futret_15)-1
gen futwret_4=(1+futret_16)*(1+futret_17)*(1+futret_18)*(1+futret_19)*(1+futret_20)-1
gen futmret_1=(1+futwret_1)*(1+futwret_2)*(1+futwret_3)*(1+futwret_4)*(1+futret_21)-1

collapse lag_convexity wret* dow_1m fut*, by(year month day monthyear)

gen obs=_n

gen fit_expect_4=.
tsset obs

*4 lags
forval i=700/3572 {
nl (dow_1m = {b0} + ///
	{b1}*(({b2}*wret_1)/({b2}+{b2}^2+{b2}^3+{b2}^4) + ///
		({b2}^2*wret_2)/({b2}+{b2}^2+{b2}^3+{b2}^4) + ///
		({b2}^3*wret_3)/({b2}+{b2}^2+{b2}^3+{b2}^4) + ///
		({b2}^4*wret_4)/({b2}+{b2}^2+{b2}^3+{b2}^4))) if obs<(`i'+1), ///
		initial(b0 0 b1 1 b2 0.5) nolog vce(cluster monthyear)

gen fit_expect=_b[/b0] + _b[/b1]*((_b[/b2]*wret_1)/(_b[/b2]+_b[/b2]^2+_b[/b2]^3+_b[/b2]^4) + ///
		(_b[/b2]^2*wret_2)/(_b[/b2]+_b[/b2]^2+_b[/b2]^3+_b[/b2]^4) + ///
		(_b[/b2]^3*wret_3)/(_b[/b2]+_b[/b2]^2+_b[/b2]^3+_b[/b2]^4) + ///
		(_b[/b2]^4*wret_4)/(_b[/b2]+_b[/b2]^2+_b[/b2]^3+_b[/b2]^4)) if obs<(`i'+1)
	
replace fit_expect_4=fit_expect if obs==`i'

drop fit_expect

}

drop if missing(fit_expect_4)

replace fit_expect_4=fit_expect_4*100
replace futmret_1=futmret_1*100
replace dow_1m=dow_1m*100

gen res_expect_4=dow_1m-fit_expect_4
egen res_expect_4_std=std(res_expect_4)
egen fit_expect_4_std=std(fit_expect_4)
egen dow_1m_std=std(dow_1m)
egen convex_std=std(lag_convexity)
}

****TABLE 10****
*coefficients and t-stats
newey futmret_1 dow_1m_std, lag(8)
newey futmret_1 fit_expect_4_std, lag(8)
newey futmret_1 res_expect_4_std, lag(8)

*R-squared
reg futmret_1 dow_1m_std
reg futmret_1 fit_expect_4_std
reg futmret_1 res_expect_4_std

****TABLE 11****
*coefficients and t-stats
newey dow_1m convex_std, lag(8)
newey fit_expect_4 convex_std, lag(8)
newey res_expect_4 convex_std, lag(8)

*R-squared
reg dow_1m convex_std
reg fit_expect_4 convex_std
reg res_expect_4 convex_std
